<!--
 * @Description: 
 * @Author: mmzhang
 * @Date: 2022-07-12 11:27:32
 * @LastEditTime: 2022-08-23 16:25:22
-->
<template>
  <dt-modal-edit ref="edit" title="请选择需分入线索的人员" :width="500" :loading="loading" @ok="onOk">
    <div>
      <p style="margin-bottom: 10px;">分配线索后，勾选线索可在分配人员的未到访客户列表查看该线索信息</p>
      <p style="margin-bottom: 10px;text-align:center;color:#ff9900" v-if="receiverFlag">
        当前线索来自于同策好房，只能分配给案场承接人</p>

      <dt-grid ref="table" :columns="columns" :onFetch="onFetch" :height="400" />
    </div>
  </dt-modal-edit>
</template>

<script>
  import columnOpt from '../opt/allotColumn.js'
  import clueCustomerMgr from '@/biz/customer/clueCustomer.js'

  export default {
    data() {
      return {
        columns: [],
        selected: null,
        loading: false,
        userCustomerId: null,
        distributionWay: '',
        userName: null,
        receiverFlag: false
      }
    },
    methods: {


      onOk() {

        if (!this.selected) {
          this.$Message.error('请选择需分入线索的人员')
          return
        }
        this.loading = true
        let params = {
          userCustomerIdList: this.userCustomerId,
          userId: this.selected,
        }
        if (this.distributionWay) {
          params.distributionWay = this.distributionWay
        }


        this.$Modal.confirm({
          title: '分配线索客户确认',
          content: `确定将${this.userCustomerId.length}个线索客户分配给${this.userName}？`,
          closable: true,
          onOk: () => {

            this.$parent.customerIds = []
            this.$parent.assignConfirmFlag = false
            clueCustomerMgr.distributeClue(params).then(res => {
              this.$Message.success('分配成功')
              this.$parent.$refs.table.fetch()
              this.$parent.customerIds = []
              this.$parent.userCustomerId = []
              this.$parent.assignConfirmFlag = false
              this.loading = false
              this.hide()
              this.$emit('detailsDistributeClue')
            }).catch(() => {
              this.loading = false
            })
          }
        })


      },


      onFetch(data) {
        // return clueCustomerMgr.adviserList()
        let params = { onlineType: 1 }
        // return clueCustomerMgr.adviserList({ params })

        return clueCustomerMgr.adviserList(params).then((res) => {
          if (this.receiverFlag) {
            res.forEach((item, index) => {
              if (item.isReceiver == 1) {
                res.unshift(res.splice(index, 1)[0]);
              }
            });
          }
          return res
        })

      },
      show(val, retain, receiverData) {
        console.log(receiverData,
          'receiverDatareceiverDatareceiverDatareceiverDatareceiverData');

        let flag = ''
        if (receiverData && receiverData.sourceType) {
          flag = receiverData.sourceType == '网络来客'
        }

        if (receiverData && (receiverData.relationClueChannel == 10008 || receiverData
            .relationClueChannel == 10009) && flag) {
          this.receiverFlag = true
          this.userCustomerId = [receiverData.userCustomerId]
        } else if (!val && receiverData.userCustomerId) {

          this.userCustomerId = [receiverData.userCustomerId]
        } else {
          this.userCustomerId = val
        }
        this.selected = null
        if (retain) {
          this.distributionWay = retain
        }

        this.$refs.edit.show()
        this.columns = columnOpt.options(this)
        this.$refs.table.init(this)
        this.$refs.table.fetch()
      },
      hide() {
        this.$refs.edit.hide()
      },
    }
  }
</script>

<style lang="scss" scoped>
</style>